<?php

class FormProducts extends App_Form  
{
	private $productData;
	
	public function __construct($product) {
		$this->productData = $product;
		parent::__construct();
	}
	/**
     * Создание формы
     */
    public function init()
    {
        // Вызываем родительский метод
        parent::init();
        global $prodCategories;
        // Указываем action формы
        $helperUrl = new Zend_View_Helper_Url();
        $this->setAction($helperUrl->url(array(), 'adminProductForm'));
        
        // Указываем метод формы
        $this->setMethod('post');
        
        // Задаем атрибут class для формы
        $this->setAttrib('class', 'commonform');
        $this->setAttrib('name', 'productform');
        
        $id = new Zend_Form_Element_Hidden('hidden', array(
        	'name'		  => 'id',
            'value'       => $this->productData->id 
        ));
        $id->setDecorators(array('Hidden'));
        $this->addElement($id);

        $catid = new Zend_Form_Element_Select('catid', array(
            'required'    => true,
			'decorators' => $this->elementDecorators,
        	'label'       => 'Категория',
            'maxlength'   => '30',
        	'multiOptions'=> $prodCategories, 
        	'value'		  => $this->productData->catid
        ));
        $catid->setDecorators(array('Standart'));
        $this->addElement($catid);
        
        $title = new Zend_Form_Element_Text('title', array(
            'required'    => true,
        	'label'       => 'Название',
            'maxlength'   => '30',
            'validators'  => array(
                array('Alnum', true, array(true)),
                array('StringLength', true, array(2, 30))
             ),
            'filters'     => array('StringTrim'),
            'value'		  => $this->productData->title
        ));
        $title->setDecorators(array('Standart'));
        $this->addElement($title);

        $price = new Zend_Form_Element_Text('price', array(
            'required'    => true,
        	'label'       => 'Цена',
            'maxlength'   => '10',
            'validators'  => array(
                array('Float', true, array("ru")),
             ),
            'filters'     => array('StringTrim'),
            'value'		  => $this->productData->price
        ));
        $price->setDecorators(array('Standart'));
        $this->addElement($price);
		
        $isplace = new Zend_Form_Element_Checkbox('isplace', array(
            'required'    => true,
        	'label'       => 'В наличии',
            'filters'     => array('StringTrim'),
            'value'		  => $this->productData->isplace
        ));
        $isplace->setDecorators(array('Standart'));
        $this->addElement($isplace);

        $fname = new App_Form_Element_Gallery('fname', array(
        	'label'       => 'Изображение',
            'value'		  => $this->productData->photo->id
        ));
    	$fname->setDestination(UPLOAD_PATH . 'products');
        $fname->addGalleryPhoto($this->productData->photo);
    	$fname->setDecorators(array('Gallery'));
        $this->addElement($fname);
        
        $text = new Zend_Form_Element_Textarea('text', array(
            'required'    => true,
        	'label'       => 'Описание',
            'filters'     => array('StringTrim'),
            'value'		  => $this->productData->text
        ));
		$text->setDecorators(array("FCKEditor"));
        $this->addElement($text);
        
		// Кнопка Submit
		$submit = new Zend_Form_Element_Submit('submit', array(
			'decorators' => $this->buttonDecorators,
			'label'       => 'Сохранить',
		));
		$this->addElement($submit);
		        
		// Кнопка Reset, возвращает форму в начальное состояние
		$reset = new Zend_Form_Element_Reset('reset', array(
			'decorators' => $this->buttonDecorators,
			'label'       => 'Очистить',
		));
		$this->addElement($reset);
    }
}
?>